#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>


//数组匹配

#include <limits.h>

// 函数来计算数组的和
int sumArray(int* array, int size) {
    int sum = 0;
    for (int i = 0; i < size; i++) {
        sum += array[i];
    }
    return sum;
}

// 主函数
int main() {
    int a[] = { 1, 2, 3 }; // 示例数组 a
    int b[] = { 4, 3, 2, 7, 5 }; // 示例数组 b

    int aSize = sizeof(a) / sizeof(a[0]);
    int bSize = sizeof(b) / sizeof(b[0]);

    int targetSum = sumArray(a, aSize); // 计算数组 a 的和

    int closestSum = INT_MAX;
    int bestStart = 0;
    int bestLength = 0;
    int minDiff = INT_MAX;

    // 使用滑动窗口技术
    for (int length = 1; length <= bSize; length++) {
        int windowSum = 0;
        for (int i = 0; i < length; i++) {
            windowSum += b[i];
        }

        for (int start = 0; start <= bSize - length; start++) {
            if (start > 0) {
                windowSum = windowSum - b[start - 1] + b[start + length - 1];
            }

            int diff = abs(windowSum - targetSum);
            if (diff < minDiff || (diff == minDiff && start < bestStart)) {
                closestSum = windowSum;
                minDiff = diff;
                bestStart = start;
                bestLength = length;
            }
        }
    }

    printf("最接近的子数组起始位置：%d\n", bestStart);
    printf("最接近的子数组长度：%d\n", bestLength);
    printf("最接近的子数组之和：%d\n", closestSum);

    return 0;
}
*/